﻿
function move(availableDdl, selectedDdl, direction, dataField) {
	var availableFields = document.getElementById(availableDdl);
	var selectedFields = document.getElementById(selectedDdl);
	var temp;
	var temp2;

	if (direction == 'up') {
		for (i = 0; i < selectedFields.options.length; i++) {
			if (selectedFields.options[i].selected == true) {
				if (i != 0) {
					temp = new Option(selectedFields.options[i - 1].text, selectedFields.options[i - 1].value);
					temp2 = new Option(selectedFields.options[i].text, selectedFields.options[i].value);
					selectedFields.options[i - 1] = temp2;
					selectedFields.options[i - 1].selected = true;
					selectedFields.options[i] = temp;
				}
			}
		}
	}
	if (direction == 'down') {
		for (i = (selectedFields.options.length - 1); i >= 0; i--) {
			if (selectedFields.options[i].selected == true) {
				if (i != (selectedFields.options.length - 1)) {
					temp = new Option(selectedFields.options[i + 1].text, selectedFields.options[i + 1].value);
					temp2 = new Option(selectedFields.options[i].text, selectedFields.options[i].value);
					selectedFields.options[i + 1] = temp2;
					selectedFields.options[i + 1].selected = true;
					selectedFields.options[i] = temp;
				}
			}
		}
	}
	if (direction == 'right') {
		for (i = 0; i < availableFields.options.length; i++) {
			if (availableFields.options[i].selected == true) {
				temp = new Option(availableFields.options[i].text, availableFields.options[i].value);
				selectedFields.add(temp);
				availableFields.remove(i);
			}
		}
	}
	if (direction == 'left') {
		for (i = 0; i < selectedFields.options.length; i++) {
			if (selectedFields.options[i].selected == true) {
				temp = new Option(selectedFields.options[i].text, selectedFields.options[i].value);
				selectedFields.remove(i);
				availableFields.add(temp);
			}
		}
	}
	if (dataField != '') {
		updateDataField(selectedDdl,dataField);
	}
	return false;
}

function updateDataField(selectedDdl, dataField) {
	var currentlySelected = document.getElementById(dataField);
	var selectedFields = document.getElementById(selectedDdl);
	var textFields = "";

	for (var i = 0; i < selectedFields.options.length; i++) {
		if (textFields == "") {
			textFields = selectedFields.options[i].value;
		}
		else {
			textFields = textFields + ',' + selectedFields.options[i].value;
		}
	}
	currentlySelected.value = textFields;
}
